Framework.instance=null;

Framework.prototype.fireLifeCycleEvent=function (eventName) {
	for (var i=0; i<this.controls.length; i++){
		if (eventName in this.controls[i]){
			this.controls[i][eventName]();
		}
	}
}

//Builds control from specified node
Framework.prototype.buildControl = function (node) {
    var jsClassName = node.getAttribute("coreimsee:jsClass");
//    ClassLoader.instance.load(jsClassName);
    var jsClass = eval(jsClassName);
    var control =  new jsClass(node);
    return control;
}

function Framework(){
	this.controls=new Array();
	this.finalizers = new Array();
	EventManager.hookupHandler(window, "load", EventManager.createHandler(this, "fireLifeCycleEvent", "onload"));
	//Unused for now
	//this.fireLifeCycleEvent("oninit");
}

Framework.prototype.getControlByName = function (nodeName) {
    if (nodeName.isNullOrEmpty()) return null;
    return this.getControlByNode(document.getElementById(nodeName));
}

Framework.prototype.getControlByNode = function (node) {
    if (node==null) return null;
//    var nodeName = node.getAttribute("id");
//    var control = this.builtControls[nodeName];
    var control = this.getNodeControl(node);
    if (!Utils.isDefined(control)){
        control = this.buildControl(node);
        this.setNodeControl(node, control);
//        node.jsObject = control;
    }
    return control;
}

Framework.prototype.getNodeControl = function (node) {
    var controlIndex = node["jsObjectIndex"];
    if (!Utils.isDefined(controlIndex)) return null;
    return this.controls[controlIndex];
}

Framework.prototype.setNodeControl = function (node, control) {
    node["jsObjectIndex"] = this.controls.length;
    this.controls.push(control);
}

Framework.prototype.isControlNode = function (node) {
    if (node.nodeType!=1) return false;
    try {
        return node.getAttribute("coreimsee:jsClass")!=null;   
    } catch (e) {
        //No idea why, but IE sometimes throws exception on node.getAttribute()
        //and sometimes don't
        return false;
    }
}

Framework.prototype.registerFinalizer = function (obj) {
    this.finalizers.push(obj);   
}

Framework.prototype.finalize = function () {
    for (var i=0; i<this.finalizers.length; i++) {
        this.finalizers[i].finalize();
    }
    this.finalizers.removeAll();
}

Framework.prototype.onunload = function () {
    this.finalize();
    return false;
}


Framework.create=function () {
	Framework.instance=new Framework();	
	EventHandler.windowEventHandler=new EventHandler(null, window);
    EventHandler.windowEventHandler.registerListener("load", {"onload": function (){document.loaded=true;}})
    EventHandler.windowEventHandler.registerListener("unload", Framework.instance);    
}